package cn.com.thingo.shyintelligent.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.CurtainInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.CurtainContract;
import cn.com.thingo.shyintelligent.presenter.CurtainPresenter;
import cn.com.thingo.shyintelligent.ui.adapter.CurtainListAdapter;
import cn.com.thingo.shyintelligent.ui.adapter.decoration.RecycleViewDivider;

public class CurtainActivity extends BaseActivity<CurtainPresenter> implements CurtainContract.View {
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.curtain_list_rv)
    RecyclerView rvList;
    private THOpenSDK thOpenSDK;
    private int currentRoomId;
    private List<CurtainInfo> curtainList = new ArrayList<>();
    private CurtainListAdapter adapter;

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        currentRoomId = getIntent().getIntExtra("current_room_id", -1);
        initView();
        initData();
    }

    private void initData() {
        List<CurtainInfo> curtainList = thOpenSDK.getCurtainList();
        if (curtainList != null && curtainList.size() > 0) {
            this.curtainList.clear();
            for (int i = 0; i < curtainList.size(); i++) {
                CurtainInfo curtainInfo = curtainList.get(i);
                if (curtainInfo.getRoomId() == currentRoomId) {
                    this.curtainList.add(curtainInfo);
                }
            }
        }
        if (this.curtainList.size() > 0) {
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.curtain);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new CurtainListAdapter(curtainList, mContext);
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        rvList.setAdapter(adapter);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_curtain;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }
}
